﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmBillBoxCheck : Form
    {
        public FrmBillBoxCheck()
        {
            InitializeComponent();
        }

        int _Cnt = 0;
        string _BoxCode = "";
        List<string> _BoxNos = new List<string>();
        List<string> _CheckBoxNos = new List<string>();

        private void FrmTCheck_Load(object sender, EventArgs e)
        {
            InitFrm();
        }

        /// <summary>
        /// 初始化画面
        /// </summary>
        private void InitFrm()
        {
            _Cnt = 0;
            _CheckBoxNos = new List<string>();
            _BoxNos = new List<string>();
            txtBillNo.Enabled = true;
            _BoxCode = "";

            txtBoxCode.Text = "";
            txtBillNo.Text = "";
            txtBoxCode.Enabled = true;
            txtBillNo.Enabled = true;
            areaInfo.Text = "";
            areaMsg.Text = "";

            this.txtBillNo.Focus();
        }


        /// <summary>
        /// 箱标签输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBoxCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (txtBillNo.Enabled == true)
                {
                    areaMsg.Text = "请先输入托标签";
                    txtBoxCode.Text = "";
                    txtBillNo.Text = "";
                    txtBillNo.Focus();
                    return;
                }

                _BoxCode = txtBoxCode.Text;
                string[] strBox = _BoxCode.Split('-');

                string msg = "";
                if (_BoxNos.Contains(strBox[0]))
                {
                    if (_CheckBoxNos.Remove(strBox[0]))
                    {
                        areaInfo.Text = GetInfo();
                        msg = "验证成功";
                    }
                    else
                    {
                        msg = "此箱号已验证";
                    }
                }
                else
                {
                    msg = "验证失败,箱号不属于该出库单";

                }
                txtBoxCode.Text = "";
                areaMsg.Text = msg;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            InitFrm();
        }


        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            DataTable info = Bll_ProductOut.GetOutStockInfo(txtBillNo.Text);
            if (info.Rows.Count == 0)
            {
                areaMsg.Text = "无此出货单";
                txtBillNo.Text = "";
                return;
            }

            var sql = "select * from wms_T_OutStockRowDetails where BillNo='{0}'";
            sql = string.Format(sql, txtBillNo.Text);
            DataTable dt = Utils.NMS.GetDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var boxNo = dt.Rows[i]["BoxNo"].ToString();
                _BoxNos.Add(boxNo);
                _CheckBoxNos.Add(boxNo);
            }

            txtBillNo.Enabled = false;
            txtBoxCode.Focus();
            areaMsg.Text = "扫描箱号，开始验证";
            areaInfo.Text = GetInfo();
        }

        private string GetInfo()
        {
            var info = "扫描箱数：{0}/" + _BoxNos.Count.ToString();
            info = string.Format(info, _Cnt.ToString());
            _Cnt++;
            return info;
        }

        //END

    }
}